/*
 * cmdlineproc.c
 *
 *  Created on: Nov 26, 2011
 *      Author: benoybose
 */

#include "hxcommon.h"
#include "hxcmdline.h"

#include <stdio.h>
#include <stdlib.h>
#include <gc.h>
#include <string.h>

const char* INPUT_FILES_TYPE = "<input>";
const char* INPUT_FILES_DESC = "Input source files.";
const char* OUTPUT_FORMAT_SHORT = "f";
const char* OUTPUT_FORMAT_LONG = "format";
const char* OUTPUT_FORMAT_TYPE = "<format>";
const char* OUTPUT_FORMAT_DESC = "Output format. x:executable l:library s:shared";
const char* OUTPUT_FILE_SHORT = "o";
const char* OUTPUT_FILE_LONG = "output";
const char* OUTPUT_FILE_TYPE = "<output>";
const char* OUTPUT_FILE_DESC = "Output binary file.";
const char* LIB_SHORT = "L";
const char* LIB_LONG = "library";
const char* LIB_TYPE = "<library>";
const char* LIB_DESC = "Path to standard library";
const char* HELP_SHORT = "h";
const char* HELP_LONG = "help";
const char* HELP_DESC = "Help, print this message.";
const char* VERSION_SHORT = "v";
const char* VERSION_LONG = "version";
const char* VERSION_DESC = "Print version information.";

const char* ARG_NULL_CHECK_FAILED = "Internal issues with arguments.";
const char* VERSION_MESSAGE = "hooc 0.1\nCopyright (c) 2011 Benoy Bose\n";

hxcmdline_options* hxcmdline_create_options() {
	hxcmdline_options* options = 0;
	options = GC_MALLOC(sizeof(hxcmdline_options));
	options->input_file = arg_filen(0, 0,
			INPUT_FILES_TYPE, 1, 1024, INPUT_FILES_DESC);
	options->output_format = arg_str0(OUTPUT_FORMAT_SHORT,
			OUTPUT_FORMAT_LONG,
			OUTPUT_FORMAT_TYPE,
			OUTPUT_FORMAT_DESC);
	options->output_file = arg_file0(OUTPUT_FILE_SHORT,
			OUTPUT_FILE_LONG,
			OUTPUT_FILE_TYPE,
			OUTPUT_FILE_DESC);
	options->library_file = arg_file0(LIB_SHORT,
			LIB_LONG,
			LIB_TYPE,
			LIB_DESC);
	options->help = arg_lit0(HELP_SHORT, HELP_LONG, HELP_DESC);
	options->version = arg_lit0(VERSION_SHORT,
			VERSION_LONG,
			VERSION_DESC);
	options->end = arg_end(10);
	return options;
}

int cmdline_parse(int argc, char** argv, hxcmdline_options* options) {
	void* argtable[] = {
			options->input_file,
			options->output_format,
			options->output_file,
			options->library_file,
			options->help,
			options->version,
			options->end
	};

	if(0 != arg_nullcheck(argtable)) {
		printf("%s\n", ARG_NULL_CHECK_FAILED);
		return (1);
	}

	int errors = arg_parse(argc, argv, argtable);
	if(0 < errors) {
		int safe_exit = 0;

		if(0 < options->version->count) {
			printf("%s\n", VERSION_MESSAGE);
			safe_exit = 1;
		}

		if(0 < options->help->count) {
			printf("Usage: %s ", PROGNAME);
			arg_print_syntax(stdout, argtable, 0);
			printf("%s", "\n\n");
			arg_print_glossary_gnu(stdout, argtable);
			safe_exit = 1;
		}

		if(safe_exit) {
			exit(0);
		}
		arg_print_errors(stdout, options->end, PROGNAME);
		printf("Usage: %s ", PROGNAME);
					arg_print_syntax(stdout, argtable, 0);
					printf("%s", "\n\n");
					arg_print_glossary_gnu(stdout, argtable);
		return (1);
	}
	return (0);
}

int hxcmdline_count_inputfiles(hxcmdline_options* options) {
	return options->input_file->count;
}

char* hxcmdline_get_inputfile(hxcmdline_options* options, int index) {
	const char* fileName = options->input_file->filename[index];
	int length = strlen(fileName);
	char* inputfile = (char*) GC_MALLOC(1 + (length * sizeof(char)));
	memset(inputfile, length + 1, sizeof(char));
	strcpy(inputfile, fileName);
	return inputfile;
}

char* hxcmdline_get_inputfile_ext(hxcmdline_options* options, int index) {
	const char* fileext = options->input_file->extension[index];
	int length = strlen(fileext);
	char* inputfileext = (char*) GC_MALLOC(1 + (length * sizeof(char)));
	memset(inputfileext, length + 1, sizeof(char));
	strcpy(inputfileext, fileext);
	return inputfileext;
}


